package com.aaa.spring.controller;


import com.aaa.spring.pojo.Result;
import com.aaa.spring.pojo.User;
import com.aaa.spring.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;

//标识这是个控制器 控制跳转的
@Controller   //相当于 将 @Controller + @ResponseBody
@RequestMapping("/user")
public class UserController {
    //声明日志变量
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired //注入
    private UserService userService;

    /**
     * 交易转账
     * @return
     */
//    @ResponseBody   //就会将实体类对象返回成json
    //GET POST PUT DELETE OPTIONS(跨域) 还有其他
    @GetMapping("/transfer") //指定get请求
//    @PostMapping("/transfer") //指定post请求
//    @PutMapping
    public Result transfer(@RequestParam Integer fromId,
                           @RequestParam Integer toId,
                           @RequestParam(defaultValue = "123") BigDecimal money) {
//                         @RequestBody User user   POST专用
        return userService.transfer(fromId, toId, money);
    }


    /**
     * 获取数据库所有用户
     * @return
     */
    @ResponseBody   //就会将实体类对象返回成json
    @GetMapping("/getAllUsers")
    public Result getAllUsers(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        Result page = userService.getAllUsers(pageNum,pageSize);
//        logger.info("分页="+page.getPageNum());
//        logger.info("分页数据="+page.getPageSize());
        return page;
    }

    //返回一个user对象
    @ResponseBody   //就会将实体类对象返回成json
    @GetMapping("/json")
    public User json() {
        User user = new User();
        user.setId(1);
        user.setName("法外狂徒");
        user.setPassword("123456");


        //日志打印
        logger.info("infoo-=====================");
        logger.debug("debug-=====================");
        logger.warn("warn-=====================");
        logger.error("error-=====================");

        return user;
    }
    //返回一个user对象
    @ResponseBody   //就会将实体类对象返回成json
    @GetMapping("/jsonList")
    public List<User> jsonList() {
        User user = new User();
        user.setId(1);
        user.setName("法外狂徒");
        user.setPassword("123456");

        User user1 = new User();
        user1.setId(2);
        user1.setName("法外狂徒2");
        user1.setPassword("12345622222");

        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user1);
        return list;
    }











    //登录业务 跳转到主页
    @RequestMapping("/loginSubmit")
    public ModelAndView loginSubmit(User user) {
        //用户登录业务判断
        boolean isLogin = userService.login(user);
        //ModelAndView 携带数据及视图信息
        ModelAndView mv = new ModelAndView();
        //设置视图
        mv.setViewName(isLogin ? "home" : "401");
        //isLogin ? "home" : "401"
//        if(isLogin){
//            mv.setViewName(  "home"  );
//        }else{
//            mv.setViewName(  "401"  );
//        }
        return mv;
    }

    //跳转到注册页面
    @GetMapping("/toRegister")
    public String toRegister() {
        return "register";
    }

    //注册业务 跳转到login页面
    @ResponseBody       //将返回对象转换成json
    @PostMapping("/registerSubmit")
    public Result registerSubmit(@RequestBody User user) {
                                        //requestBody是接收post请求的js对象
        //注册
        return userService.register(user);
    }



    @GetMapping("toUserList")
    public ModelAndView toUserList(){
        ModelAndView mv = new ModelAndView();
        //把用户列表带到视图
//        List<User> list = userService.listUser();
        //把数据传递给页面
//        mv.addObject("ul",list);
        mv.setViewName("userList");
        return mv;
    }






}
